#include "Queue.h"





void QueueInit(Queue* pt)
{
	assert(pt);

	pt->head = pt->tail = NULL;
	pt->size = 0;
}

void QueueDestroy(Queue* pt)
{
	assert(pt);
	QNode* cur = pt->head;

	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pt->head = pt->tail = NULL;
	pt->size = 0;
}


void QueuePush(Queue* pt, QDataType x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}

	newnode->next = NULL;
	newnode->data = x;

	if (pt->head == NULL)
	{
		assert(pt->tail == NULL);
		pt->head = pt->tail = newnode;
	}
	else
	{
		pt->tail->next = newnode;
		pt->tail = newnode;
	}

	pt->size++;

}


void QueuePop(Queue* pt)
{
	assert(pt);
	assert(pt->head != NULL);

	if (pt->head->next == NULL)
	{
		free(pt->head);
		pt->head = pt->tail = NULL;
	}
	else
	{
		QNode* next = pt->head->next;

		free(pt->head);
		pt->head = next;
	}
	pt->size--;
}


int QueueSize(Queue* pt)
{
	assert(pt);
	
	return pt->size;
}

bool QueueEmpty(Queue* pt)
{
	assert(pt);
	
	return pt->size == 0;
}

QDataType QueueFront(Queue* pt)
{
	assert(pt);
	assert(!QueueEmpty(pt));

	return pt->head->data;
}
QDataType QueueBack(Queue* pt)
{
	assert(pt);
	assert(!QueueEmpty(pt));

	return pt->tail->data;
}